(function(win, doc) {
    
    var iconIdPrefix = '__kxjp_share_cnt_',
        gid = 0,
        plcs = getPlcs();
    
    for(var i=0, len=plcs.length; i<len; i++) {
        showShareIcon(plcs[i]);
    }
    
    function getPlcs() {
        var plcs = [], ele,
            divs = doc.getElementsByTagName('div'),
            regExp = new RegExp("(^|\\s)kxjp\-shareicon(\\s|$)");
        for(var i=0, len=divs.length; i<len; i++) {
            ele = divs[i];
            if(regExp.test(ele.className)) {
                plcs.push(ele);
            }
        }
        return plcs;
    }
    
    function showShareIcon(plc) {
        var shape = plc.getAttribute('shape'),
            size = plc.getAttribute('size'),
            withnum = plc.getAttribute('withnum') || 0,
            title = plc.getAttribute('title') || doc.title,
            url = doc.location.href,
            pic = plc.getAttribute('pic') || '',
            source = plc.getAttribute('source') || url,
            price = plc.getAttribute('price') || '';
            
        // 在plc处生成icon
        var iconCnt = doc.createElement('span'),
            iconRect = getIconRect(shape, size, withnum);
        iconCnt.id = iconIdPrefix + gid;
        gid++;
        plc.parentNode.replaceChild(iconCnt, plc);
        iconCnt.style.cssText = 'display:inline-block;overflow:hidden;text-indent:0;margin:0;padding:0;background:none;border:none;float:none;line-height:normal;font-size:1px;vertical-align:baseline;';
        iconCnt.style.width = iconRect.width+'px';
        iconCnt.style.height = iconRect.height+'px';
        
        // 嵌入iframe
        var iconIframe = doc.createElement('iframe');
        iconIframe.width = '100%';
        iconIframe.height = '100%';
        iconIframe.scrolling = 'no';
        iconIframe.border = '0';
        iconIframe.frameBorder = '0';
        iconIframe.allowTransparency = 'true';
        iconIframe.style.border = 'none';
        iconCnt.appendChild(iconIframe);
        iconIframe.src = 'http://{_WWW_HOST_}/share/plug.php?shape='+shape+'&size='+size+'&withnum='+withnum+'&title='+encodeURIComponent(title)+'&url='+encodeURIComponent(url)+'&pic='+encodeURIComponent(pic)+'&source='+encodeURIComponent(source)+'&price='+encodeURIComponent(price);
    }
    
    function getIconRect(shape, size, withnum) {
        var iconWidth = {'c':{'s':20, 'm':30, 'b':35}, 's':{'s':20, 'm':30, 'b':35}, 'b':{'s':55, 'm':65, 'b':95}},
            numWidth = {'s':65, 'm':75, 'b':85},
            height = {'s':16, 'm':25, 'b':33};
        return {
            'width' : (iconWidth[shape] && iconWidth[shape][size] || iconWidth['c']['s']) + (withnum ? (numWidth[size] || numWidth['s']) : 0),
            'height' : height[size] || height['s']
        };
    }
    
}(window, document));